﻿@using System.Globalization
@using MvcFront.Models
@using Telerik.Web.Mvc.UI
@using MvcFront.DB
@model IEnumerable<FileLibraryFolder>
           
@{
    ViewBag.Title = "Библиотека Редактор";
}
    @Html.ValidationSummary(true)
<table><tr>
           <td>
                <h3>Раздел</h3>
               @(Html.Telerik().TreeView().ExpandAll(true)
           .Name("FoldersTreeView")
           .ClientEvents(events => events.OnSelect("onFolderSelected"))
                   .BindTo(Model, mappings => mappings.For<FileLibraryFolder>(binding => binding
                                                                               .ItemDataBound((item, category) =>
                                                                                                  {
                                                                                                      item.Text = category.Name;
                                                                                                      item.Value = category.filelibraryfolderid.ToString(CultureInfo.InvariantCulture);
                                                                                                  })
                                                                               .Children(category => category.ChildFileLibraryFolders)))
                     ) 
           </td>
           <td>
               @(Html.Telerik().Grid<FileLibraryAssetListViewModel>()
                .Name("AssetsListGrid")
                .DataBinding(dataBinding => dataBinding.Ajax().Select("_AssetsFromFolder", "Library"))
                                        .ClientEvents(events => events
                                                    .OnComplete("onGridComplete")
                                                    .OnDataBinding("onGridDataBinding"))
                   .Columns(columns =>
                   {
                       columns.AutoGenerate(column =>
                       {
                           column.Width = "150px";
                           if (column.Member == "AssetId" || column.Member == "FileName")
                               column.Visible = false;
                       });
                       columns.Command(command =>
                                           {
                                               command.Custom("DeleteDetails").Text("Удалить")
                                                  .DataRouteValues(route => route.Add(o => o.AssetId).RouteKey("id"))
                                                  .Action("DeleteAsset", "Library")
                                                  .Ajax(true)
                                                  .HtmlAttributes(new { style = "text-align: center" });
                                               command.Custom("viewDownload").Text("Скачать")
                                                   .DataRouteValues(route => route.Add(o => o.AssetId).RouteKey("assetId"))
                                                   .Action("Download", "Library")
                                                   .HtmlAttributes(new {style = "text-align: center"});
                                           }).Width(50);
                   })
                           .Sortable()
                           .Pageable()

    ) 
           </td>
       </tr>
</table>

 @(Html.Telerik().Grid<FileLibraryFolderListViewModel>()
                .Name("AssetFoldersListGrid")
                          .DataBinding(dataBinding => dataBinding.Ajax().Select("_AssetFoldersGridList", "Library"))
                   .Columns(columns =>
                                {
                                    columns.AutoGenerate(column =>
                                                             {
                                                                 column.Width = "150px";
                                                                 if (column.Member == "AssetFolderId")
                                                                     column.Visible = false;
                                                             });
                                    columns.Command(command =>
                                                        {
                                                            command.Custom("editDetails").Text("Редактировать")
                                                                .DataRouteValues(route => route.Add(o => o.AssetFolderId).RouteKey("id"))
                                                                .Action("EditAssetFolder", "Library")
                                                                .HtmlAttributes(new {style = "text-align: center"});
                                                            command.Custom("AddDocumentDetails").Text("Добавить документ")
                                                                .DataRouteValues(route => route.Add(o => o.AssetFolderId).RouteKey("id"))
                                                                .Action("CreateAsset", "Library")
                                                                .HtmlAttributes(new { style = "text-align: center" });
                                                        }).Width(50);
                       
                   })
                           .Sortable()
                           .Pageable()

       ) 
<p>
        @Html.ActionLink("Добавить Папку","CreateAssetFolder")
</p>
<script type="text/javascript">

    $(document).ready(function () {
        window.selectedFolder = 0;
    });
    window.selectedFolder = 0;
    
    function rebindGrid() {
        var grid = $('#AssetsListGrid').data("tGrid");
        if (grid != null) {
            grid.rebind();
        }
    }

    function onFolderSelected(e) {
        window.selectedFolder = $('#FoldersTreeView').data('tTreeView').getItemValue(e.item);
        rebindGrid();
    }
    
    function onGridDataBinding(e) {
        e.data = { id: window.selectedFolder };
    }

    function onGridComplete(e) {
        if (e.name == "DeleteDetails") {
            rebindGrid();
        }
    }
    
</script>
